package org.crud4j.ejb3.example;

import javax.naming.InitialContext;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;
import org.crud4j.ejb3.Ejb3BeanProvider;
import org.crud4j.ejb3.Ejb3CrudService;

/**
 * Startup servlet. It registers the example bean provider in crud4j
 */
public class Crud4jStartupServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private static Logger log = Logger.getLogger(Crud4jStartupServlet.class);

	@Override
	public void init(ServletConfig cfg) throws ServletException {
		try {
			InitialContext ctx = new InitialContext();
			Ejb3BeanProvider provider = (Ejb3BeanProvider) ctx.lookup(cfg
					.getInitParameter("provider-jndi"));
			Ejb3CrudService.registerProvider(provider);
		} catch (Exception e) {
			log.error("unable to register provider. "
					+ cfg.getInitParameter("provider-jndi"), e);
			throw new ServletException(e);
		}
	}

}
